//
//  FindViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "FindViewController.h"
#import "AfNetWorkingHandler.h"
#import "FindsModel.h"
#import "FindDetailCell.h"
#import "FindDetailModel.h"
#import <UIImageView+WebCache.h>
#import "PushWebViewVC.h"
#import <MJRefresh.h>
#import "BlackManager.h"
#import "SetBlack.h"
@interface FindViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *findArr;
@end

@implementation FindViewController
- (void)dealloc
{
    [_path release];
    [_titleName release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    self.navigationItem.title = _titleName;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-newlisticon06"] style:UIBarButtonItemStylePlain target:self action:@selector(fanhui)];
    [_tableView registerClass:[FindDetailCell class] forCellReuseIdentifier:@"Findcell"];
    [self.view addSubview:_tableView];
    //数据处理
    [self dataHandler];
    //下拉刷新
    //话题页下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    [_tableView release];
    
    // 上拉加载
//    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
//    if (_findArr.count != 0) {
//        [_tableView.mj_footer beginRefreshing];
//    }
    
}
//上拉加载
- (void)loadnewdata
{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval a=[dat timeIntervalSince1970]*1000;
    NSString *timeString = [NSString stringWithFormat:@"%f", a];
    
////    NSString *str = [NSString stringWithFormat:@"http://dis.myzaker.com/api/get_post.php?_appid=iphone&_bsize=640_1136&_dev=iPhone%2C8.4.1&_idfa=B04A9F17-39FD-4ADF-9D92-9F93CE548EFE&_lat=38.882504&_lbs_city=%E5%A4%A7%E8%BF%9E&_lbs_province=%E8%BE%BD%E5%AE%81&_lng=121.539787&_mac=02%3A00%3A00%3A00%3A00%3A00&_net=wifi&_udid=ECFC153B-2246-4EB0-8D9F-510516697A0C&_uid=&_utoken=&_v=6.4.6&_version=6.45&act=next&discussion_id=83&order=0&order_type=last_comment&sort_type=last_comment&timestamp=%@", timeString] ;
//    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
//        NSArray *arr = result[@"data"][@"posts"];
//        NSArray *arrTemp = [NSArray array];
//        arrTemp = [FindDetailModel modelHanderWithArray:arr];
//        for (NSInteger i = 0; i < 10; i++) {
//            [_findArr addObject:arrTemp[i]];
//        }
//        [_tableView reloadData];
//    }];
    [_tableView.mj_footer endRefreshing];
    
}
//返回方法
-(void)fanhui
{
    [self dismissViewControllerAnimated:YES completion:^{
        //
    }];
}
//数据处理
-(void)dataHandler
{
    [AfNetWorkingHandler GetWithStr:_path complection:^(id result) {
        //
        NSArray *arr = result[@"data"][@"posts"];
        self.findArr = [FindDetailModel modelHanderWithArray:arr];
        [_tableView reloadData];
    }];
    [_tableView.mj_header endRefreshing];
}
//点击进入详情webView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PushWebViewVC *detailVC = [[PushWebViewVC alloc]init];
    FindDetailModel *m = _findArr[indexPath.row];
    detailVC.pathstr = m.weburl;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailModel *m = _findArr[indexPath.row];
    CGFloat h = [FindDetailCell heightWithText:m.content];
    return h + 300;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _findArr.count;
}
//页面出现之前更新
-(void)viewWillAppear:(BOOL)animated
{
    [_tableView reloadData];
}
-(void)setLightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
}
-(void)setNightDayModel
{
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FindDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Findcell" forIndexPath:indexPath];
    [SetBlack setCellColorWithCell:cell];
    [SetBlack setLableTitleColorWithArr:@[cell.contentLable]];
    FindDetailModel *m = _findArr[indexPath.row];
    [cell.userImg sd_setImageWithURL:[NSURL URLWithString:m.autherImg]];
    cell.userLable.text = m.autherName;
    cell.timeLable.text = m.date;
    cell.contentLable.text = m.content;
    cell.content = m.content;
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:m.imgPath]];
    cell.hotLabel.text = m.hot_num;
    cell.likeLable.text = m.like_num;
    cell.messageLable.text = m.comment_count;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
